package com.hbsx.why.ui;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.hbsx.why.R;
import com.hbsx.why.base.BaseActivity;
import com.hbsx.why.http.HttpAdapter;
import com.hbsx.why.http.HttpFileUtils;
import com.hbsx.why.http.OnResponseListener;
import com.hbsx.why.model.BaseMessageModel;
import com.hbsx.why.utils.LogUtils;
import com.hbsx.why.utils.ToastUtils;
import com.hbsx.why.utils.refresh.PullScrollView;
import com.hbsx.why.utils.refresh.PullToRefreshLayout;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.tools.PictureFileUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;

/**
 * Created by liub on 2017/8/3 17:02.
 */

public class DemoPhotoPickTake extends BaseActivity {
    @BindView(R.id.tv_topTitle)
    TextView tvTopTitle;
    @BindView(R.id.result)
    TextView resultTextView;
    @BindView(R.id.take_photo)
    Button takePhoto;
    @BindView(R.id.pick_photo)
    Button pickPhoto;
    @BindView(R.id.demo_main_scrollview)
    PullScrollView demo_main_scrollview;
    @BindView(R.id.img1)
    ImageView ImageView1;
    @BindView(R.id.img2)
    ImageView ImageView2;
    @BindView(R.id.pullToRefreshLayout)
    PullToRefreshLayout pullToRefreshLayout;
    private List<LocalMedia> selectList;
    private Context context = this;

    @Override
    protected int getLayoutResId() {
        return R.layout.demo_photo_pick_take;
    }

    @Override
    protected void initView() {
        tvTopTitle.setText("拍照&&选择照片");
        selectList = new ArrayList<>();
    }

    @OnClick({R.id.take_photo, R.id.pick_photo, R.id.pick_photo_look, R.id.pick_photo_video})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.take_photo:
                PictureSelector.create(DemoPhotoPickTake.this)
                        .openGallery(PictureMimeType.ofImage())//图库选择
                        .compress(true)
                        .enableCrop(true)
                        .compressMaxKB(100)
                        .forResult(PictureConfig.CHOOSE_REQUEST);

                break;
            case R.id.pick_photo:
                PictureSelector.create(DemoPhotoPickTake.this)
                        .openCamera(PictureMimeType.ofImage())//相机拍摄PictureMimeType.ofImage()图片
                        .compress(true)
                        .enableCrop(true)
                        .compressMaxKB(100)
                        .forResult(PictureConfig.CHOOSE_REQUEST);
                break;
            case R.id.pick_photo_look:
                //PictureSelector.create(DemoPhotoPickTake.this).externalPicturePreview(position, "/custom_file", selectList);
                // PictureSelector.create(DemoPhotoPickTake.this).externalPicturePreview(position, selectList);
                break;
            case R.id.pick_photo_video:
                if (selectList.size() != 0) {
                    fileupload(selectList);
                } else {
                    ToastUtils.toastShort("请先选择图片！");
                }

                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片选择结果回调
                    selectList = PictureSelector.obtainMultipleResult(data);
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
                    //adapter.setList(selectList);
                    //adapter.notifyDataSetChanged();
                    LogUtils.logi("---------onActivityResult:" + selectList.size());
                    if (selectList.size() == 1) {
                        Glide.with(context).load(selectList.get(0).getPath()).into(ImageView1);
                        LogUtils.logi("---------selectList.get(0).getCompressPath():" + selectList.get(0).getCompressPath());
                        LogUtils.logi("---------selectList.get(0).getCutPath():" + selectList.get(0).getCutPath());//true
                        LogUtils.logi("---------selectList.get(0).getPath():" + selectList.get(0).getPath());
                        LogUtils.logi("---------selectList.get(0).isCompressed():" + selectList.get(0).isCompressed());
                    } else if (selectList.size() > 1) {
                        Glide.with(context).load(selectList.get(0).getCompressPath()).into(ImageView1);
                        Glide.with(context).load(selectList.get(1).getPath()).into(ImageView2);
                    }
                    break;
            }
        }
    }

    private void fileupload(List<LocalMedia> images) {
        List<String> filePath = new ArrayList<>();
        for (LocalMedia localMedia : images) {
            filePath.add(localMedia.getCompressPath());
        }
        RequestBody requestFile = HttpFileUtils.filesToMultipartBody("img", filePath, MediaType.parse("multipart/form-data"));
        Call<BaseMessageModel> call = HttpAdapter.getSerives().fileuploadBody(requestFile);
        call.enqueue(new OnResponseListener<BaseMessageModel>(this) {
            @Override
            protected void onSuccess(BaseMessageModel baseMessageModel) {
                Toast.makeText(context, baseMessageModel.getMsg(), Toast.LENGTH_SHORT).show();
                resultTextView.setText("结果：" + baseMessageModel.toString());
            }

            @Override
            protected void onFailure(String msg) {
                super.onFailure(msg);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        PictureFileUtils.deleteCacheDirFile(DemoPhotoPickTake.this);
    }
}
